package biz.datalk.industrialland.common.advice.cfg;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.util.HashMap;
import java.util.Map;

/**
 * @author tarofang@163.com
 * @date 2019-09-24
 */
@Getter
@Setter
@ConfigurationProperties(prefix = DatalkCryptoCfgs.PREFIX)
public class DatalkCryptoCfgs {
    public static final String PREFIX = "datalk.crypto";

    private boolean enable;
    private boolean requestDecrypt;
    private boolean responseEncrypt;
    private String aesKey;
    @NestedConfigurationProperty
    private DatalkKeyPair server;
    @NestedConfigurationProperty
    private DatalkKeyPair client;

    // add 2022-06-28 by tarofang@163.com S
    private boolean defaultGm = false;
    private boolean forceGm = false;
    @NestedConfigurationProperty
    private DatalkKeyPair gmServer;
    @NestedConfigurationProperty
    private DatalkKeyPair gmClient;
    // private String sm4Key; // default CBC key
    /**
     * key: xClientType  客户端类型
     * val: xClientVer   客户端版本，只要大于等于该版本，就使用国密加密
     */
    private Map<String, Integer> gmClientInfo = new HashMap<>();

    /** appkey 是否参与签名 */
    private Boolean needAppkeySign = Boolean.TRUE;

    public boolean useGm(String xClientType, int xClientVer) {
        if (forceGm) {
            return true;
        }
        int size = gmClientInfo == null ? 0 : gmClientInfo.size();
        if (size <= 0) {
            return defaultGm;
        }
        if (StringUtils.isBlank(xClientType)) {
            return defaultGm;
        }

        Integer clientVer = gmClientInfo.get(xClientType.trim());
        if (clientVer == null) {
            return defaultGm;
        }

        return xClientVer >= clientVer;
    }
    // add 2022-06-28 by tarofang@163.com E

}
